import { _decorator, bits, Collider2D, Color, Component, Contact2DType, Node, toDegree } from 'cc';
import { Const } from './Const';
import { Constant } from './Constant';
import { Util } from './Util';
import { Weapon } from './Weapon';
import { Sword } from './Sword';
import { Lightning } from './Lightning';
import { Bullet } from './Bullet';
import { BuffHolder } from './Buff';
const { ccclass, property } = _decorator;

@ccclass('Enemy')
export class Enemy extends Component {
    @property(Node)
    private ndAnim: Node;

    speed: number = 1.2;
    moveDirection: number = 0;
    isMoving: boolean = false;
    hp: number = 100;

    private buffHolder = new BuffHolder();

    protected onEnable(): void {
        const collider = this.node.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }
        this.hp = 100;

        this.buffHolder.onCallBack((uid: number, value: number, color: string) => {
            this.takeDamage(value, color);
        })
    }

    onBeginContact(self: Collider2D, other: Collider2D) {
        if (other.group === Constant.ColliderGroup.PLAYER_WEAPON) {
            switch (other.tag) {
                case Constant.WeaponTag.ARROW:
                    this.takeDamage(other.node.getComponent(Weapon).attack);
                    break;
                case Constant.WeaponTag.SWORD:
                    this.takeDamage(other.node.getComponent(Sword).attack);
                    break;
                case Constant.WeaponTag.FIREBAll:
                    this.takeDamage(other.node.getComponent(Weapon).attack);
                    break;
                case Constant.WeaponTag.LIGHTNING:
                    this.takeDamage(other.node.getComponent(Lightning).attack);
                    this.buffHolder.AddBuff(0, '#1E2BC3', 20, 5, 1);
                    break;
                case Constant.WeaponTag.BULLET:
                    this.takeDamage(other.node.getComponent(Bullet).attack);
                    break;
                default:
                    break;
            }
        }
    }

    takeDamage(damage: number, color: string = '#FF0000') {
        this.hp -= damage;
        Util.showDamageText(`${damage}`, color, this.node.worldPosition, Const.ndDamageTextParent);
        if (this.hp <= 0) {
            this.node.destroy();
        }
    }




    onEndContact(self: Collider2D, other: Collider2D) {

    }

    protected onDisable(): void {
        const collider = this.node.getComponent(Collider2D);
        if (collider) {
            collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            collider.off(Contact2DType.END_CONTACT, this.onEndContact, this);

        }
    }

    start() {
        this.isMoving = true;
        this.schedule(() => {
            if (Const.ndPlayer == null || !Const.ndPlayer.isValid) return;
            const deltaX = Const.ndPlayer.worldPosition.x - this.node.worldPosition.x;
            const deltaY = Const.ndPlayer.worldPosition.y - this.node.worldPosition.y;
            this.moveDirection = Math.atan2(deltaY, deltaX);
        }, 0.1)
    }

    update(deltaTime: number) {
        this.buffHolder.update(deltaTime);

        if (this.isMoving) {
            const x = this.node.position.x + Math.cos(this.moveDirection) * this.speed;
            const y = this.node.position.y + Math.sin(this.moveDirection) * this.speed;
            this.node.setPosition(x, y);

            const degree = toDegree(this.moveDirection);
            if (degree >= -90 && degree <= 90) {
                this.ndAnim.setScale(1, 1)
            } else {
                this.ndAnim.setScale(-1, 1)
            }
        }

    }

}


